﻿using B2B.Bussiness;
using B2B.ServiceReference1;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace B2B.Views
{
    public partial class SiparisSonuc : ChildWindow
    {
        public List<Order> SiparisListe { get; set; }
        DataServiceClient client = new DataServiceClient();
        Kullanici usr = Session.Instance.GetValue<Kullanici>("USER");
        private bool goruntule = false;
        public bool Goruntule { get { return goruntule; } set { goruntule = value; } }

        public SiparisSonuc()
        {
            InitializeComponent();
            this.Loaded += Siparis_Loaded;
        }

        void Siparis_Loaded(object sender, RoutedEventArgs e)
        {
            grdSiparis.ItemsSource = SiparisListe;
            if (goruntule)
            {
                btnBitir.Visibility = System.Windows.Visibility.Collapsed;
                btnSil.Visibility = System.Windows.Visibility.Collapsed;
            }
            CalcSubTotal();
        }

        void CalcSubTotal()
        {
            double aratop = SiparisListe.Select(x => x.Yekun).Sum();
            double isk = SiparisListe.Select(x => (x.Iskonto1 + x.Iskonto2 + x.Iskonto3 + x.Iskonto4 + x.Iskonto5 + x.Iskonto6) * x.Miktar).Sum();
            double kdv = SiparisListe.Select(x => x.KdvTutari).Sum();
            txtYekun.Text = (aratop - isk + kdv).ToString();
            txtAraToplam.Text = aratop.ToString();
            txtIndirim.Text = isk.ToString();
            txtKdv.Text = kdv.ToString();
        }

        private void btnSil_Click_1(object sender, RoutedEventArgs e)
        {
            List<Order> sil = new List<Order>();
            foreach (var item in SiparisListe)
            {
                if (item.Sec)
                {
                    sil.Add(item);
                }
            }
            foreach (var item in sil)
            {
                SiparisListe.Remove(item);
            }
            grdSiparis.ItemsSource = null;
            grdSiparis.ItemsSource = SiparisListe;
            CalcSubTotal();
        }

        private void btnBitir_Click(object sender, RoutedEventArgs e)
        {
            ObservableCollection<Siparis> lines = new ObservableCollection<Siparis>();
            int satir = 0;
            foreach (Order item in SiparisListe)
            {
                Siparis sp = new Siparis();
                sp.Aciklama = txtAciklama.Text;
                sp.CariKodu = item.CariKodu;
                sp.Fiyat = item.Fiyat;
                sp.Iskonto1 = item.Iskonto1;
                sp.Iskonto2 = item.Iskonto2;
                sp.Iskonto3 = item.Iskonto3;
                sp.Iskonto4 = item.Iskonto4;
                sp.Iskonto5 = item.Iskonto5;
                sp.Iskonto6 = item.Iskonto6;
                sp.KdvID = item.KdvID;
                sp.KdvOran = item.KdvOrani;
                sp.KdvTutar = item.KdvTutari;
                sp.KullaniciID = item.KullaniciID;
                sp.Miktar = item.Miktar;
                sp.Onaylandi = usr.SiparisiOnayli;
                if (usr.SiparisiOnayli)
                {
                    sp.OnaylayanKullaniciID = usr.ID;
                }
                sp.OnaylayanKullaniciID = -1;
                sp.Satir = satir;
                sp.Seri = "SIP";
                sp.Sira = 1;
                sp.StokKodu = item.StokKodu;
                sp.Tarih = DateTime.Now;
                sp.Tutar = item.Yekun;
                lines.Add(sp);
                satir++;
            }
            client.SiparisEkleAsync(lines, true);
            client.SiparisEkleCompleted += client_SiparisEkleCompleted;
        }

        void client_SiparisEkleCompleted(object sender, SiparisEkleCompletedEventArgs e)
        {
            if (e.Result)
            {
                MessageBox.Show("Siparişiniz Kaydedildi !");
                SiparisListe.Clear();
                this.Close();
            }
            else
            {
                string mesaj = "Evrak Kaydedilemedi !";
                if (e.Error != null)
                    mesaj += "\r\n" + e.Error.Message;
                MessageBox.Show(mesaj);
            }
        }

        private void txtMiktar_TextChanged_1(object sender, TextChangedEventArgs e)
        {
            var ctl = sender as TextBox;
            if (null != ctl)
            {
                var ord = ctl.Tag as Order;
                if (null != ord)
                {
                    ord.Miktar = Convert.ToDouble(ctl.Text);
                }
            }
            CalcSubTotal();
        }
    }
}

